﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class DepartmentForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            BranchEntity branchE = new BranchEntity();
            BranchService branchS = BranchService.GetInstance();
            IList<BranchEntity> lstBranch = branchS.LoadAll();
            if (null != lstBranch)
            {
                this.storeBranch.DataSource = lstBranch;
                this.storeBranch.DataBind();
            }
        }
        public void Show(string departmentId)
        {
            DepartmentEntity departmentE = new DepartmentEntity();
            DepartmentService departmentS = DepartmentService.GetInstance();
            departmentE.DepartmentId = int.Parse(departmentId);
            departmentE = departmentS.LoadByPrimaryKey(departmentE);
            if (null == departmentE)
                return;
            hdfDepartmentId.Value = departmentE.DepartmentId;
            txtDepartmentName.Text = departmentE.DepartmentName;            
            cbbBranch.SetValue(departmentE.BranchId);
            txtDescription.Text = departmentE.Description;
            ckbIsActive.Checked = departmentE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            DepartmentEntity departmentE = new DepartmentEntity();
            DepartmentService departmentS = DepartmentService.GetInstance();
            if ("" != hdfDepartmentId.Value.ToString())
            {
                string departmentId = hdfDepartmentId.Value.ToString();
                departmentE.DepartmentId = int.Parse(departmentId);
                departmentE = departmentS.LoadByPrimaryKey(departmentE);
                departmentE.DepartmentName = txtDepartmentName.Text;                
                departmentE.BranchId = int.Parse(cbbBranch.SelectedItem.Value);
                departmentE.Description = txtDescription.Text;
                departmentE.IsActive = ckbIsActive.Checked;
                departmentE.UpdatedDate = DateTime.Now;
                departmentE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                departmentS.Update(departmentE);
            }
            else
            {
                departmentE.DepartmentName = txtDepartmentName.Text;                
                departmentE.BranchId = int.Parse(cbbBranch.SelectedItem.Value);
                departmentE.Description = txtDescription.Text;
                departmentE.IsActive = ckbIsActive.Checked;
                departmentE.CreatedDate = DateTime.Now;
                departmentE.CreatedBy = FSSContext.Current.User.EmployeeId;
                departmentE.UpdatedDate = DateTime.Now;
                departmentE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                departmentS.Insert(departmentE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}